import { BloodEnum } from "src/common/enums/blood.enum";
import { GenderEnum } from "src/common/enums/gender.enum";
import { UserStatusEnum } from "src/common/enums/status.enum";
import { Picture } from "src/core/music/picture/entities";
import { User } from "src/core/system/user/entities/user.entity";
import { Column, CreateDateColumn, Entity, Index, JoinTable, ManyToMany, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { Album } from "../../album/entities/album.entity";
import { Song } from "../../song/entities/song.entity";
import { Video } from "../../video/entities/video.entity";
@Entity({ name: 'artist' })
@Index(['name','introduce'], { fulltext: true }) //分词搜索
export class Artist {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        type: "date",
        comment: "艺术家的生日"
    })
    birthday: Date


    @Column({
        type: "varchar",
        comment: "头像"
    })
    pic: string;

    @Column({
        type: "enum",
        enum: GenderEnum,
        default: GenderEnum.UNknow,
        comment: "性别"
    })
    gender: GenderEnum

    @Column({
        type: "longtext",
        comment: "艺术家的个人介绍"
    })
    introduce: string

    @Column({
        type: "int",
        comment: "艺术家的体重"
    })
    weight: number


    @Column({
        type: "enum",
        enum: BloodEnum,
        default: BloodEnum.未知,
        comment: "艺术家的血型"
    })
    blood: BloodEnum

    @Column({
        name: "artist_code",
        type: "varchar",
        comment: "艺术家的唯一编码"
    })
    artistCode: string

    @Column({
        name: "birth_place",
        type: "varchar",
        comment: "艺术家的出生地"
    })
    birthPlace: string

    @Column({
        name: "native_list",
        type: "simple-array",
        comment: "艺术家的国家地区"
    })
    nativeList: string[]

    @Column({
        type: "varchar",
        comment: "艺术家的名字"
    })
    name: string

    @Column({
        type: "varchar",
        name: "pinyin_name",
        comment: "艺术家的名字"
    })
    pinyinName: string

    @Column({
        type: "varchar",
        comment: "艺术家所在地区"
    })
    region: string

    @Column({
        type: 'decimal',
        precision: 10, //整数位数
        scale: 1, //保留一个小数点
        comment: "艺术家的身高"
    })
    height: number

    @Column({
        type: 'enum',
        enum: UserStatusEnum,
        default: UserStatusEnum.NORMAL,
        comment: "艺术家的账号状态"
    })
    status: UserStatusEnum


    @Column({
        type: 'int',
        default: 0,
        comment: "是否是删除状态"
    })
    deleted: number


    @CreateDateColumn({
        name: 'create_time'
    })
    createTime: Date; //歌手创建时间

    @UpdateDateColumn({
        name: 'update_time'
    })
    updateTime: Date; //歌手信息更新时间


    // @BeforeInsert()
    // insertArtistName() {
    //     this.pinyinName = pinyin(this.name, { toneType: 'none' });
    // }
    // @BeforeUpdate()
    // updateArtistName() {
    //     this.pinyinName = pinyin(this.name, { toneType: 'none' });
    // }

    /**
     * 关联歌手的照片墙
     */
    @OneToMany(() => Picture, picture => picture.artist, {
        cascade: ['insert', 'update', 'soft-remove', 'remove', 'recover']
    })
    pictures: Picture[]

    /**
     * 关联歌手的专辑
     */
    @ManyToMany(() => Album, album => album.artistList, {
        cascade: ['insert', 'update']
    })
    @JoinTable({
        name: 'artist_album', //生成的表名称
        joinColumn: {
            name: 'artist_id',  //生成的列名称
            referencedColumnName: 'id' //关联的原始列名称
        },
        inverseJoinColumn: {
            name: "album_id",
            referencedColumnName: "id"
        }
    })
    albums: Album[]

    /**
     * 关联歌手的歌曲
     */
    @ManyToMany(() => Song, song => song.artistList)
    @JoinTable({
        name: 'artist_song', //生成的表名称
        joinColumn: {
            name: 'artist_id',  //生成的列名称
            referencedColumnName: 'id' //关联的原始列名称
        },
        inverseJoinColumn: {
            name: "song_id",
            referencedColumnName: "id"
        }
    })
    songList: Song[]

    /**
     * 关联歌手的MV
     */
    @ManyToMany(() => Video,
        video => video.artistList,
        {
            cascade: ['insert']
        }
    )
    @JoinTable({
        name: 'artist_video', //生成的表名称
        joinColumn: {
            name: 'artist_id',  //生成的列名称
            referencedColumnName: 'id' //关联的原始列名称
        },
        inverseJoinColumn: {
            name: "video_id",
            referencedColumnName: "id"
        }
    })
    video: Video[]


    //喜欢此歌手的人
    @ManyToMany(() => User, user => user.favorArtistList, {
        cascade: true,
    })
    @JoinTable({
        name: "artist_user_favor", //生成的中间表名
        joinColumn: {
            name: "artist_id", //生成的列名
            referencedColumnName: "id" //需要关联的列
        },
        inverseJoinColumn: {
            name: "user_id", //需要被关联的生成的列名
            referencedColumnName: "id"//需要关联的列
        }
    })
    favoriteUsers: User[];

    /**
     * @typedef  {Object} Artist
     * @property {string} birthday
     * @property {string[]} pictorialList
     * @property {string} gender
     * @property {string} introduce
     * @property {number} weight
     * @property {string} pic
     * @property {string} blood
     * @property {string} artistCode
     * @property {string} birthPlace
     * @property {string} name
     * @property {string} region
     * @property {number} height
     * @property {number} albumTotal
     * @property {number} trackTotal
     * @property {number} videoTotal
     * @property {number} isFavorite
     * @property {number} favoriteCount
     */
    constructor({ id, birthday, pictorialList, gender, introduce, weight, pic, blood, artistCode, birthPlace, name, region, height } = {} as any) {
        this.birthday = birthday;

        this.gender = gender;
        this.introduce = introduce;
        this.weight = weight;

        this.blood = blood;
        this.artistCode = artistCode;
        this.birthPlace = birthPlace;
        this.name = name;
        this.region = region;
        this.height = height;
        this.id = id
    }
}


